package robotinterface;

/**
 *
 * Nate Hemmes
 */
import dialogs.About;
import dialogs.Help;
import dialogs.Run;
import dialogs.Save;
import dialogs.Load;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import robotinterface.client.RobotClient;

/*
 * This class exists solely to show you what menus look like.
 * It has no menu-related event handling.
 */
public class MenuBar {

    JTextArea output;
    JScrollPane scrollPane;

    public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu, submenu, clientMenu;
        JMenuItem menuItem, menuSave, menuLoad;
        JRadioButtonMenuItem rbMenuItem;
        JCheckBoxMenuItem cbMenuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(menu);

        //a group of JMenuItems
        menuLoad = new JMenuItem("Load", KeyEvent.VK_L);

        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        menuLoad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.ALT_MASK));
        menuLoad.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
        menuLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aa) {
                try {
                    Load dialog = new Load();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
        menu.add(menuLoad);

        menuSave = new JMenuItem("Save", KeyEvent.VK_S);
        menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
        menuSave.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
        menuSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae)
			{
				try
				{
					Save dialog = new Save();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				}
				catch (Exception exp) { exp.printStackTrace(); }
			}
		});
        menu.add(menuSave);

//        ImageIcon icon = createImageIcon("images/middle.gif");
//        menuItem = new JMenuItem("Both text and icon", icon);
//        menuItem.setMnemonic(KeyEvent.VK_B);
//        menu.add(menuItem);
//
//        menuItem = new JMenuItem(icon);
//        menuItem.setMnemonic(KeyEvent.VK_D);
//        menu.add(menuItem);

        //a group of radio button menu items
        menu.addSeparator();
        ButtonGroup group = new ButtonGroup();

//        rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
//        rbMenuItem.setSelected(true);
//        rbMenuItem.setMnemonic(KeyEvent.VK_R);
//        group.add(rbMenuItem);
//        menu.add(rbMenuItem);
//
//        rbMenuItem = new JRadioButtonMenuItem("Another one");
//        rbMenuItem.setMnemonic(KeyEvent.VK_O);
//        group.add(rbMenuItem);
//        menu.add(rbMenuItem);

        //a group of check box menu items
        menu.addSeparator();
        cbMenuItem = new JCheckBoxMenuItem("Immediate Robot Response");
        cbMenuItem.setMnemonic(KeyEvent.VK_I);
        menu.add(cbMenuItem);

        cbMenuItem = new JCheckBoxMenuItem("Safe Motion");
        cbMenuItem.setMnemonic(KeyEvent.VK_M);
        menu.add(cbMenuItem);

        //a submenu
        menu.addSeparator();
        submenu = new JMenu("Options");
        submenu.setMnemonic(KeyEvent.VK_O);

        menuItem = new JMenuItem("Robot");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
        submenu.add(menuItem);

        menuItem = new JMenuItem("Display");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
        submenu.add(menuItem);
        menu.add(submenu);


        //Build second menu in the menu bar.
        menu = new JMenu("Run");
        menu.setMnemonic(KeyEvent.VK_1);
        menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
        menuBar.add(menu);


        JMenuItem runMenu = new JMenuItem("Run");
        runMenu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
        runMenu.setMnemonic(KeyEvent.VK_R);
        runMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    Run dialog = new Run();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
        menu.add(runMenu);
        menu.addSeparator();


        runMenu = new JMenuItem("Step Forward");
        runMenu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
        runMenu.setMnemonic(KeyEvent.VK_F);
        menu.add(runMenu);

        runMenu = new JMenuItem("Step Back");
        runMenu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
        runMenu.setMnemonic(KeyEvent.VK_B);
        menu.add(runMenu);
        menu.addSeparator();

        runMenu = new JMenuItem("Stop");
        runMenu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
        runMenu.setMnemonic(KeyEvent.VK_T);
        menu.add(runMenu);

        menu = new JMenu("Tools");
        menu.setMnemonic(KeyEvent.VK_T);
        menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");


        //menu.addSeparator();
        clientMenu = new JMenu("Client");
        clientMenu.setMnemonic(KeyEvent.VK_S);
        ButtonGroup clientGroup = new ButtonGroup();

        JRadioButtonMenuItem rbClientStart = new JRadioButtonMenuItem("Start Client");
        rbClientStart.setSelected(false);
        rbClientStart.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
        //rbClientStart.setMnemonic(KeyEvent.VK_R);
        rbClientStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    RobotClient.startClient();
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
        clientGroup.add(rbClientStart);
        clientMenu.add(rbClientStart);

        JRadioButtonMenuItem rbClientStop = new JRadioButtonMenuItem("Stop Client");
        rbClientStop.setSelected(true);
        rbClientStop.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4, ActionEvent.ALT_MASK));
        //rbClientStop.setMnemonic(KeyEvent.VK_R);
        rbClientStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    RobotClient.stopClient();
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
        clientGroup.add(rbClientStop);
        clientMenu.add(rbClientStop);

        menu.add(clientMenu);
        menuBar.add(menu);


        menu = new JMenu("Help");
        menu.setMnemonic(KeyEvent.VK_H);
        menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");

        JMenuItem helpMenu = new JMenuItem("Help");
        helpMenu.setMnemonic(KeyEvent.VK_H);
        helpMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Help dialog = new Help();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
        menu.add(helpMenu);

        JMenuItem aboutMenu = new JMenuItem("About");
        aboutMenu.setMnemonic(KeyEvent.VK_A);
        aboutMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    About dialog = new About();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
        menu.add(aboutMenu);

        menuBar.add(menu);

        return menuBar;
    }
//    public Container createContentPane() {
//        //Create the content-pane-to-be.
//        JPanel contentPane = new JPanel(new BorderLayout());
//        contentPane.setOpaque(true);
//
//        //Create a scrolled text area.
//        output = new JTextArea(5, 30);
//        output.setEditable(false);
//        scrollPane = new JScrollPane(output);
//
//        //Add the text area to the content pane.
//        contentPane.add(scrollPane, BorderLayout.CENTER);
//
//        return contentPane;
//    }
//
//    /** Returns an ImageIcon, or null if the path was invalid. */
//    protected static ImageIcon createImageIcon(String path) {
//        java.net.URL imgURL = MenuLookDemo.class.getResource(path);
//        if (imgURL != null) {
//            return new ImageIcon(imgURL);
//        } else {
//            System.err.println("Couldn't find file: " + path);
//            return null;
//        }
//    }
}
